/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

/**
 * 字符串过滤并截取一定长度
 * @author lcfgly
 */
public class CharFilter {
    
    public static String getNoHTMLString(String content, int p) {

        if (null == content) {
            return "";
        }
        if (0 == p) {
            return "";
        }

        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;

        try {
            content=getNoScriptString(content);
            content=getNoHTMLString(content);
            //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }    
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(content);
            content = m_style.replaceAll(""); //过滤style标签    
        } catch (Exception e) {
            return "";
        }
        if (content.length() > p) {
            content = content.substring(0, p) + "...";
        } else {
            content = content + "...";
        }
        return content;
    }
    
    /**
     * 过滤script
     * @param content
     * @return 
     */
    public static String getNoScriptString(String content)
    {
        if (null == content)
            return "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(content);
            content = m_script.replaceAll(""); //过滤script标签 
            return content;
        } catch (Exception e) {
            return "";
        }
    }
    
    /**
     * 过滤html标记
     * @param content
     * @return 
     */
    public static String getNoHTMLString(String content)
    {
        if (null == content)
            return "";
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;

        try {
            //定义HTML标签的正则表达式    
            String regEx_html = "<[^>]+>"; 
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(content);
            content = m_html.replaceAll(""); //过滤html标签    
            return content;
        } catch (Exception e) {
            return "";
        }
    }
    
    /**
     * 获取requst提交的数据
     * @param request
     * @return 
     */
    public static Map getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name ;
        Object value;
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                value = (String[]) valueObj;
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
    
}
